﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'P_CreateAsignMenuBasicReferenceTypes')
	BEGIN
		DROP  Procedure  esf_sso.P_CreateAsignMenuBasicReferenceTypes
	END

GO

CREATE Procedure esf_sso.P_CreateAsignMenuBasicReferenceTypes
	@pApplicationCode					VARCHAR (60)
	,@pUserGroupCode					VARCHAR (60) = NULL
	,@pParentMenuCode					VARCHAR (60)
	,@pStartRangeBasicReferenceTypeID	INT
	,@pEndRangeBasicReferenceTypeID		INT
AS

	DECLARE @tmpID INT
	DECLARE @tmpName VARCHAR (60)
	DECLARE @tmpMenuCode VARCHAR (60)
	DECLARE @tmpMenuName VARCHAR (60)
	DECLARE @tmpMenuURL VARCHAR (500)
	DECLARE @ApplicationID INT
	
	
	DECLARE curBasRefMen CURSOR FOR
		SELECT
			ID
			,Name
			,@pParentMenuCode + Name
			,Name
			,'/sso/Admin/BasicReference/BrowseBasicReferences.aspx?BasicReferenceTypeID=' + CONVERT (VARCHAR (10), ID)
		FROM
			[esf_sso].BasicReferenceType 
		WHERE
			ID BETWEEN @pStartRangeBasicReferenceTypeID AND @pEndRangeBasicReferenceTypeID
			AND ID NOT IN (6, 7) -- User Menus, Enterprises

	OPEN curBasRefMen;

	FETCH NEXT FROM curBasRefMen 
	INTO @tmpID, @tmpName, @tmpMenuCode, @tmpMenuName, @tmpMenuURL;

	WHILE @@FETCH_STATUS = 0
	BEGIN

		DECLARE @xID INT

		EXEC [esf_sso].P_CreateAsignUserMenu @xID OUT, @pApplicationCode, @pUserGroupCode, @pParentMenuCode, @tmpMenuCode, @tmpMenuName, NULL, NULL, @tmpMenuURL

		FETCH NEXT FROM curBasRefMen 
		INTO @tmpID, @tmpName, @tmpMenuCode, @tmpMenuName, @tmpMenuURL;

	END

	CLOSE curBasRefMen
	DEALLOCATE curBasRefMen

GO